﻿Imports System
Imports System.Data
Imports System.String
Imports System.Text
Imports System.Collections
Imports System.Reflection
Imports System.IO



Public Class Frm_PURQuoteManage

    Implements IDServer.IDServer.IDServer


    Public ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp
    Private WithEvents MainForm As Frm_MainForm
    Private WithEvents Combox1 As New ComboBox
    Private WithEvents Combox2 As New ComboBox
    Private WithEvents cmbStartSite As New ComboBox
    Private WithEvents cmbEndSite As New ComboBox

    Const WM_NCLBUTTONDOWN As Integer = &HA1S
    Const HTCAPTION As Integer = 2
    Private MouseStartPoint As Point
    Private MouseMoving As Boolean
    Private IsSelected As Boolean
    Private bFind As Boolean
    Private SelectIndex As Integer



#Region "Property"

    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property

#End Region


#Region "interface define"


    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd
        If AddEnable = False Then
            MessageBox.Show("對不起，您沒有新增權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Function
        End If

        Me.TabControl1.SelectedIndex = 1
        SetControlEnable(True)
        ClearControlContent()
        ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd
        MainForm.BtnEnable(False)

    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel

        ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
        ClearControlContent()
        SetControlEnable(False)
        If Me.TabControl1.SelectedIndex = 1 Then
            If Me.dgv_Master.Rows.Count > 0 Then
                FillControl(Me.dgv_Master.CurrentRow.Cells("QuoteNo").Value)
            End If
        Else
            bFind = True
        End If
        MainForm.BtnEnable(bFind)

    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete

        Try
            If DeleteEnable = False Then
                MessageBox.Show("對不起，您沒有刪除權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            Dim QuoteNo As String = ""
            If Me.dgv_Master.Rows.Count > 0 Then
                QuoteNo = Me.dgv_Master.CurrentRow.Cells("QuoteNo").Value

                If MsgBox(My.resources.ConfirmDelete, MsgBoxStyle.Question + MsgBoxStyle.YesNo, Msg.CompanyName) = MsgBoxResult.Yes Then
                    If DeleteDataSet(QuoteNo) = True Then
                        MsgBox(My.Resources.DeleteSuccess, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                        If Me.TabControl1.SelectedIndex = 1 Then
                            If Me.dgv_Master.Rows.Count > 0 Then
                                FillControl(Me.dgv_Master.CurrentRow.Cells("QuoteNo").Value)
                            End If
                        End If
                    End If
                End If
            End If
        Catch ex As Exception
            MsgBox("ActionDelete:" & ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit
        If EditEnable = False Then
            MessageBox.Show("對不起，您沒有修改權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Function
        End If

        Me.TabControl1.SelectedIndex = 1
        ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit
        SetControlEnable(True)
        FillControl(Me.txtQuoteNo.Text)
        MainForm.BtnEnable(False)

    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

        If Me.dgv.Rows.Count <= 0 Then
            MsgBox("不能存儲空的數據！", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
            Exit Function
        End If

        Return True

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave
        BtnActionSave()
    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function


#End Region


#Region "sub or function"

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub BindMasterGrid()

        Dim mTable As New DataTable
        Try

            With mTable.Columns
                .Add("QuoteNo")
                .Add("CustomerNo")
                .Add("Store")
                .Add("StartDate")
                .Add("EndDate")
                .Add("Remark")
                .Add("CreateDate")
                .Add("CreatePerson")
                .Add("CreateComputer")
            End With

            Me.dgv_Master.DataSource = mTable

            With Me.dgv_Master
                For i As Integer = 0 To Me.dgv_Master.Columns.Count - 1
                    Dim mKey As String = .Columns(i).Name
                    Dim CaptionStr As String = ReturnHash(0).Item(mKey)
                    If ReturnHash(0).ContainsKey(mKey) = True Then
                        .Columns(i).HeaderText = CaptionStr
                    End If
                Next
                .AllowUserToAddRows = False
                .AllowUserToDeleteRows = False
                .SelectionMode = DataGridViewSelectionMode.FullRowSelect
                .MultiSelect = False
            End With
        Catch ex As Exception

        Finally
            mTable.Dispose()
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub BindGridDetail()

        Dim mTable As New DataTable

        Try
            With mTable.Columns
                '.Add("ItemNo")
                .Add("StartSite")
                .Add("EndSite")
                .Add("KmSum")
                .Add("Type1")
                .Add("Type2")
                .Add("UnitPrice")
                .Add("Amount")
                .Add("SumPrice")
            End With

            Me.dgv.DataSource = mTable

            With Me.dgv
                For i As Integer = 0 To Me.dgv.Columns.Count - 1
                    Dim mKey As String = .Columns(i).Name
                    Dim CaptionStr As String = ReturnHash(1).Item(mKey)
                    If ReturnHash(1).ContainsKey(mKey) = True Then
                        .Columns(i).HeaderText = CaptionStr
                    End If
                Next
                .MultiSelect = False
                .Columns("UnitPrice").DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight
                .Columns("UnitPrice").DefaultCellStyle.Format = "C"
                .Columns("Amount").DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight
                .Columns("Amount").DefaultCellStyle.BackColor = Color.AliceBlue
                .Columns("SumPrice").DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight
                .Columns("SumPrice").DefaultCellStyle.BackColor = Color.LightYellow
            End With
        Catch ex As Exception
            MsgBox("BindGridDetail:" & ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mTable.Dispose()
        End Try


    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="index"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function ReturnHash(ByVal index As Integer) As Hashtable

        Dim mHash As New Hashtable
        Try

            With mHash
                Select Case index
                    Case 0
                        .Add("QuoteNo", "報價單號")
                        .Add("CustomerNo", "客戶")
                        .Add("Store", "門市\店鋪")
                        .Add("StartDate", "開始日期")
                        .Add("EndDate", "結束日期")
                        .Add("Remark", "備注")
                        .Add("CreateDate", "建立日期")
                        .Add("CreatePerson", "建立人員")
                        .Add("CreateComputer", "建立機器")
                    Case 1
                        '.Add("ItemNo", "序號")
                        .Add("StartSite", "起始站")
                        .Add("EndSite", "目的地")
                        .Add("KmSum", "公裏數")
                        .Add("Type1", "零擔")
                        .Add("Type2", "整車")
                        .Add("UnitPrice", "單位價格")
                        .Add("Amount", "數量")
                        .Add("SumPrice", "總金額")
                End Select

            End With
            Return mHash

        Catch ex As Exception
            Throw ex
        Finally
            mHash = Nothing
        End Try

    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadDataType()

        Dim sql As String = ""
        Dim Str() As String
        Dim m_Ds As New DataSet
        sql += "select Code,Cnname from dbo.BSC_Customer;"
        sql += "select fStationId,fStationName from dbo.Lo_TranStation;"
        sql += "select fStationId,fStationName from dbo.Lo_TranStation;"
        sql += "select fCode,fShortName from dbo.BSC_StoreHouse"
        Try
            m_Ds = ReturnDataset(sql)


            If m_Ds.Tables(0).Rows.Count > 0 Then

                Me.cmbCustomer.DisplayMember = "Cnname"
                Me.cmbCustomer.ValueMember = "Code"
                Me.cmbCustomer.DataSource = m_Ds.Tables(0)
                Me.cmbCustomer.SelectedIndex = -1

                Me.cmbSelectCustomer.DisplayMember = "Cnname"
                Me.cmbSelectCustomer.ValueMember = "Code"
                Me.cmbSelectCustomer.DataSource = m_Ds.Tables(0)
                Me.cmbSelectCustomer.SelectedIndex = -1

                Me.cmbStartSite.DisplayMember = "fStationName"
                Me.cmbStartSite.ValueMember = "fStationId"
                Me.cmbStartSite.DataSource = m_Ds.Tables(1)
                Me.cmbStartSite.DropDownStyle = ComboBoxStyle.DropDownList


                Me.cmbEndSite.DisplayMember = "fStationName"
                Me.cmbEndSite.ValueMember = "fStationId"
                Me.cmbEndSite.DataSource = m_Ds.Tables(2)
                Me.cmbEndSite.DropDownStyle = ComboBoxStyle.DropDownList

                Me.cmbStore.DisplayMember = "fShortName"
                Me.cmbStore.ValueMember = "fCode"
                Me.cmbStore.DataSource = m_Ds.Tables(3)
                Me.cmbStore.DropDownStyle = ComboBoxStyle.DropDownList

                Me.cmbSelectStore.DisplayMember = "fShortName"
                Me.cmbSelectStore.ValueMember = "fCode"
                Me.cmbSelectStore.DataSource = m_Ds.Tables(3)
                Me.cmbSelectStore.DropDownStyle = ComboBoxStyle.DropDownList


            End If

        Catch ex As Exception
            MsgBox("LoadDataType:" & ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            m_Ds.Dispose()
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="B"></param>
    ''' <remarks></remarks>
    Private Sub SetControlEnable(ByVal B As Boolean)
        Me.cmbCustomer.Enabled = B
        Me.cmbStore.Enabled = B
        Me.txtStartDate.Enabled = B
        Me.txtEndDate.Enabled = B
        Me.txtRemark.Enabled = B
        'Me.txtStoreSqureSum.Enabled = B
        Me.dgv.AllowUserToAddRows = B
        Me.dgv.AllowUserToDeleteRows = B
        Me.txtServerCount.Enabled = B
        Me.btnComputer.Enabled = B
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ClearControlContent()
        Me.txtQuoteNo.Text = ""
        If Me.cmbCustomer.SelectedIndex <> -1 Then
            Me.cmbCustomer.SelectedIndex = -1
        End If
        If Me.cmbStore.SelectedIndex <> -1 Then
            Me.cmbStore.SelectedIndex = -1
        End If
        Me.txtStartDate.Value = FormatDate(Now.Date)
        Me.txtEndDate.Value = FormatDate(Now.Date)
        Me.txtRemark.Text = ""
        Me.txtStoreMoney.Text = ""
        Me.txtStorePrice.Text = ""
        Me.txtStoreSqureSum.Text = ""
        Me.txtServerCount.Text = ""
        Me.TextBox1.Text = ""
        Me.TextBox4.Text = ""
        Me.TextBox6.Text = ""
        Me.ListView1.Items.Clear()
        Dim nTable As DataTable = CType(Me.dgv.DataSource, DataTable)
        If nTable.Rows.Count > 0 Then
            nTable.Rows.Clear()
            Me.dgv.DataSource = nTable
        End If
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub BtnActionSave()

        If ActionInitialize() = False Then Exit Sub

        Dim sql As String = ""
        Dim Sqlstr As String = ""
        Dim QuoteNo As String = ""
        Dim ItemNo As String = ""
        Dim StartSite As String = ""
        Dim EndSite As String = ""
        Dim TransType As String = ""
        Dim KmSum As String = ""
        Dim Type1 As String = ""
        Dim Type2 As String = ""
        Dim UnitPrice As Double = 0
        Dim Amount As Double = 0
        Dim SumPrice As Double = 0

        Dim sqlConn As New SqlClient.SqlConnection
        Dim sqlcmd As New SqlClient.SqlCommand


        sqlConn = GetConn()
        If sqlConn.State = ConnectionState.Closed Then
            sqlConn.Open()
        End If
        Dim transqlserver As SqlClient.SqlTransaction = sqlConn.BeginTransaction
        Try

            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
                QuoteNo = AutoCreateQuoteNo(Me.cmbStore.SelectedValue.ToString, "dbo.Logic_QuoteManageA")

                sql = "Insert into Logic_QuoteManageA(QuoteNo, CustomerNo,Store, StartDate, EndDate, Remark, CreateDate, CreatePerson, CreateComputer) values('"
                sql += QuoteNo & "','" & Me.cmbCustomer.SelectedValue.ToString & "','" & Me.cmbStore.SelectedValue.ToString & "','" & FormatDate(Me.txtStartDate.Value) & "','" & FormatDate(Me.txtEndDate.Value)
                sql += "','" & Me.txtRemark.Text & "','" & ServerDate & "','" & PubUserName & "','" & My.Computer.Name & "')"

                With sqlcmd
                    .Connection = sqlConn
                    .Transaction = transqlserver
                    .CommandType = CommandType.Text
                    .CommandText = sql
                    .ExecuteNonQuery()
                End With
                sql = ""
                sql = "Insert into Logic_QuoteManageB(QuoteNo, QuoteType,ItemNo,ItemA, ItemB, ItemC, ItemD,ItemE,ItemF, Amount, SumMoney)" & vbCrLf

                If Me.dgv.Rows.Count > 0 Then
                    For i As Integer = 0 To Me.dgv.Rows.Count - 1
                        If Me.dgv.Rows(i).IsNewRow Then Exit For
                        StartSite = Me.dgv.Rows(i).Cells("StartSite").Value
                        EndSite = Me.dgv.Rows(i).Cells("EndSite").Value
                        KmSum = Me.dgv.Rows(i).Cells("KmSum").Value
                        UnitPrice = Double.Parse(Me.dgv.Rows(i).Cells("UnitPrice").Value)
                        Amount = Convert.ToDouble(Me.dgv.Rows(i).Cells("Amount").Value)
                        SumPrice = Double.Parse(Me.dgv.Rows(i).Cells("SumPrice").Value)
                        If Not Me.dgv.Rows(i).Cells("Type1").Tag Is Nothing Then
                            TransType = Me.dgv.Rows(i).Cells("Type1").Tag
                            Type1 = Me.dgv.Rows(i).Cells("Type1").Value
                        End If
                        If Not Me.dgv.Rows(i).Cells("Type2").Tag Is Nothing Then
                            TransType = Me.dgv.Rows(i).Cells("Type2").Tag
                            Type2 = Me.dgv.Rows(i).Cells("Type2").Value
                        End If

                        Sqlstr += "select '" & QuoteNo & "','TransQuote','" & Me.LabStoreQuote.Text & "','" & StartSite & "','" & EndSite & "','" & KmSum & "','" & Type1 & "','" & Type2 & "','" & UnitPrice & "'," & Amount & "," & SumPrice & "" & vbCrLf
                        Sqlstr += "union all" & vbCrLf
                    Next
                End If

                If Me.ListView1.Items.Count > 0 Then
                    For i As Integer = 0 To Me.ListView1.Items.Count - 1
                        ItemNo = Me.ListView1.Items(i).Text
                        Dim ItemName As String = Me.ListView1.Items(i).SubItems(1).Text
                        Dim ItemPrice As String = Me.ListView1.Items(i).SubItems(2).Text
                        If i = Me.ListView1.Items.Count - 1 Then
                            Sqlstr += "select '" & QuoteNo & "','ServerQuote','" & Me.LabStoreQuote.Text & "','" & ItemNo & "','" & ItemName & "','','','','" & ItemPrice & "'," & Double.Parse(Me.ListView1.Items(i).SubItems(3).Text) & "," & Double.Parse(Me.ListView1.Items(i).SubItems(4).Text) & vbCrLf
                        Else
                            Sqlstr += "select '" & QuoteNo & "','ServerQuote','" & Me.LabStoreQuote.Text & "','" & ItemNo & "','" & ItemName & "','','','','" & ItemPrice & "'," & Double.Parse(Me.ListView1.Items(i).SubItems(3).Text) & "," & Double.Parse(Me.ListView1.Items(i).SubItems(4).Text) & vbCrLf
                            Sqlstr += "union all" & vbCrLf
                        End If
                    Next
                End If

                sql = sql + Sqlstr

                With sqlcmd
                    .Connection = sqlConn
                    .Transaction = transqlserver
                    .CommandType = CommandType.Text
                    .CommandText = sql
                    .ExecuteNonQuery()
                End With
                AddToDataSet(QuoteNo, Me.cmbCustomer.Text, FormatDate(Me.txtStartDate.Value), FormatDate(Me.txtEndDate.Value), Me.cmbStore.Text, Me.txtRemark.Text.Trim, ServerDate, PubUserCode, My.Computer.Name)
                Me.txtQuoteNo.Text = QuoteNo
            Else

                sql = "update Logic_QuoteManageA set CustomerNo='" & Me.cmbCustomer.SelectedValue.ToString & "',StartDate='" & FormatDate(Me.txtStartDate.Value) & "',EndDate='" & FormatDate(Me.txtEndDate.Value) & "',Store='"
                sql += Me.cmbStore.SelectedValue.ToString & "',Remark='" & Me.txtRemark.Text & "',LastUpdatePerson='" & PubUserCode & "',LastUpdateComputer='" & My.Computer.Name & "',LastUpdateDate='" & Now.Date & "' "
                sql += " where QuoteNo='" & Me.txtQuoteNo.Text & "';"
                sql += "delete Logic_QuoteManageB where QuoteNo='" & Me.txtQuoteNo.Text & "'"

                With sqlcmd
                    .Connection = sqlConn
                    .Transaction = transqlserver
                    .CommandType = CommandType.Text
                    .CommandText = sql
                    .ExecuteNonQuery()
                End With

                sql = ""
                sql = "Insert into Logic_QuoteManageB(QuoteNo, QuoteType,ItemNo,ItemA, ItemB, ItemC, ItemD,ItemE,ItemF, Amount, SumMoney)" & vbCrLf

                If Me.dgv.Rows.Count > 0 Then
                    For i As Integer = 0 To Me.dgv.Rows.Count - 1
                        If Me.dgv.Rows(i).IsNewRow Then Exit For
                        StartSite = Me.dgv.Rows(i).Cells("StartSite").Value
                        EndSite = Me.dgv.Rows(i).Cells("EndSite").Value
                        KmSum = Me.dgv.Rows(i).Cells("KmSum").Value
                        UnitPrice = Double.Parse(Me.dgv.Rows(i).Cells("UnitPrice").Value)
                        Amount = Convert.ToDouble(Me.dgv.Rows(i).Cells("Amount").Value)
                        SumPrice = Double.Parse(Me.dgv.Rows(i).Cells("SumPrice").Value)
                        If Not Me.dgv.Rows(i).Cells("Type1").Tag Is Nothing Then
                            TransType = Me.dgv.Rows(i).Cells("Type1").Tag
                            Type1 = Me.dgv.Rows(i).Cells("Type1").Value
                        End If
                        If Not Me.dgv.Rows(i).Cells("Type2").Tag Is Nothing Then
                            TransType = Me.dgv.Rows(i).Cells("Type2").Tag
                            Type2 = Me.dgv.Rows(i).Cells("Type2").Value
                        End If

                        Sqlstr += "select '" & Me.txtQuoteNo.Text & "','TransQuote','" & Me.LabStoreQuote.Text & "','" & StartSite & "','" & EndSite & "','" & KmSum & "','" & Type1 & "','" & Type2 & "','" & UnitPrice & "'," & Amount & "," & SumPrice & "" & vbCrLf
                        Sqlstr += "union all" & vbCrLf
                    Next
                End If

                If Me.ListView1.Items.Count > 0 Then
                    For i As Integer = 0 To Me.ListView1.Items.Count - 1
                        ItemNo = Me.ListView1.Items(i).Text
                        Dim ItemName As String = Me.ListView1.Items(i).SubItems(1).Text
                        Dim ItemPrice As String = Me.ListView1.Items(i).SubItems(2).Text
                        If i = Me.ListView1.Items.Count - 1 Then
                            Sqlstr += "select '" & Me.txtQuoteNo.Text & "','ServerQuote','" & Me.LabStoreQuote.Text & "','" & ItemNo & "','" & ItemName & "','','','','" & ItemPrice & "'," & Double.Parse(Me.ListView1.Items(i).SubItems(2).Text) & "," & Double.Parse(Me.ListView1.Items(i).SubItems(3).Text) & vbCrLf
                        Else
                            Sqlstr += "select '" & Me.txtQuoteNo.Text & "','ServerQuote','" & Me.LabStoreQuote.Text & "','" & ItemNo & "','" & ItemName & "','','','','" & ItemPrice & "'," & Double.Parse(Me.ListView1.Items(i).SubItems(2).Text) & "," & Double.Parse(Me.ListView1.Items(i).SubItems(3).Text) & vbCrLf
                            Sqlstr += "union all" & vbCrLf
                        End If
                    Next
                End If

                sql = sql + Sqlstr

                With sqlcmd
                    .Connection = sqlConn
                    .Transaction = transqlserver
                    .CommandType = CommandType.Text
                    .CommandText = sql
                    .ExecuteNonQuery()
                End With
                EditToDataSet(QuoteNo, Me.cmbCustomer.Text, FormatDate(Me.txtStartDate.Value), FormatDate(Me.txtEndDate.Value), Me.cmbStore.Text, Me.txtRemark.Text.Trim, ServerDate, PubUserCode, My.Computer.Name)
            End If

            transqlserver.Commit()
            sqlConn.Close()

            MsgBox(IIf(ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd, My.resources.Addsuccess, My.resources.EditSuccess), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)

            ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
            SetControlEnable(False)
            bFind = True
            MainForm.BtnEnable(bFind)
        Catch ex As Exception
            transqlserver.Rollback()
            MsgBox("BtnActionSave:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            sqlConn.Close()
            sqlcmd.Dispose()
            transqlserver.Dispose()
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="QuoteNo"></param>
    ''' <param name="CustomerNo"></param>
    ''' <param name="StartDate"></param>
    ''' <param name="EndDate"></param>
    ''' <param name="Store"></param>
    ''' <param name="Remark"></param>
    ''' <param name="CreateDate"></param>
    ''' <param name="CreatePerson"></param>
    ''' <param name="CreateComputer"></param>
    ''' <remarks></remarks>
    Private Sub AddToDataSet(ByVal QuoteNo As String, ByVal CustomerNo As String, ByVal StartDate As String, ByVal EndDate As String, ByVal Store As String, ByVal Remark As String, ByVal CreateDate As String, ByVal CreatePerson As String, ByVal CreateComputer As String)

        Dim mTable As DataTable = CType(Me.dgv_Master.DataSource, DataTable)
        Dim NewRow As DataRow = mTable.NewRow
        Try
            NewRow("QuoteNo") = QuoteNo
            NewRow("CustomerNo") = CustomerNo
            NewRow("StartDate") = StartDate
            NewRow("EndDate") = EndDate
            NewRow("Remark") = Remark
            NewRow("Store") = Store
            NewRow("CreateDate") = CreateDate
            NewRow("CreatePerson") = CreatePerson
            'NewRow("CreateComputer") = CreateComputer

            mTable.Rows.Add(NewRow)
            Me.dgv_Master.DataSource = mTable
        Catch ex As Exception
            MsgBox("AddToDataSet" & ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mTable.Dispose()
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="QuoteNo"></param>
    ''' <param name="CustomerNo"></param>
    ''' <param name="StartDate"></param>
    ''' <param name="EndDate"></param>
    ''' <param name="Store"></param>
    ''' <param name="Remark"></param>
    ''' <param name="CreateDate"></param>
    ''' <param name="CreatePerson"></param>
    ''' <param name="CreateComputer"></param>
    ''' <remarks></remarks>
    Private Sub EditToDataSet(ByVal QuoteNo As String, ByVal CustomerNo As String, ByVal StartDate As String, ByVal EndDate As String, ByVal Store As String, ByVal Remark As String, ByVal CreateDate As String, ByVal CreatePerson As String, ByVal CreateComputer As String)

        Dim mTable As DataTable = CType(Me.dgv_Master.DataSource, DataTable)
        Dim Rows() As DataRow = mTable.Select("QuoteNo='" & QuoteNo & "'")
        Try
            If Rows.Length > 0 Then
                Rows(0).Item("CustomerNo") = CustomerNo
                Rows(0).Item("Store") = Store
                Rows(0).Item("StartDate") = StartDate
                Rows(0).Item("EndDate") = EndDate
                Rows(0).Item("Remark") = Remark
                Rows(0).Item("CreateDate") = CreateDate
                Rows(0).Item("CreatePerson") = CreatePerson
                'Rows(0).Item("CreateComputer") = CreateComputer

                mTable.AcceptChanges()
                Me.dgv_Master.DataSource = mTable

            End If
        Catch ex As Exception
            MsgBox("EditToDataSet" & ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mTable.Dispose()
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="QuoteNo"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function DeleteDataSet(ByVal QuoteNo As String) As Boolean

        Dim sql As String = ""
        Dim mTable As DataTable = CType(Me.dgv_Master.DataSource, DataTable)
        sql = "Delete Logic_QuoteManageA where QuoteNo='" & QuoteNo & "';Delete Logic_QuoteManageB where QuoteNo='" & QuoteNo & "'"
        Try
            Dim Rows() As DataRow = mTable.Select("QuoteNo='" & QuoteNo & "'")
            If Rows.Length > 0 Then
                mTable.Rows.Remove(Rows(0))
                ReturnObject(sql)
                Return True
            End If
        Catch ex As Exception
            Return False
        Finally
            mTable.Dispose()
        End Try
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub FillControl(ByVal QuoteNo As String)


        ClearControlContent()
        Dim sql As String = ""
        Me.txtQuoteNo.Text = QuoteNo
        Me.cmbCustomer.SelectedIndex = Me.cmbCustomer.FindString(Me.dgv_Master.CurrentRow.Cells("CustomerNo").Value)
        Me.cmbStore.SelectedIndex = Me.cmbStore.FindString(Me.dgv_Master.CurrentRow.Cells("Store").Value)
        Me.txtRemark.Text = Me.dgv_Master.CurrentRow.Cells("Remark").Value

        sql = "select StartSite=ItemA,EndSite=ItemB,KmSum=ItemC,Type1=ItemD,Type2=ItemE,UnitPrice=ItemF,Amount=Amount,SumPrice=SumMoney from Logic_QuoteManageB where QuoteType='TransQuote' and QuoteNo='" & QuoteNo & "';"
        sql += "select ItemA,ItemB,ItemF,Amount,SumMoney from Logic_QuoteManageB where QuoteType='ServerQuote' and QuoteNo='" & QuoteNo & "'"
        Dim m_Ds As New DataSet
        Try
            m_Ds = ReturnDataset(sql)
            Me.dgv.DataSource = m_Ds.Tables(0)

            If m_Ds.Tables(1).Rows.Count > 0 Then
                Me.ListView1.Items.Clear()
                For Each mRow As DataRow In m_Ds.Tables(1).Rows
                    Dim Item As New ListViewItem
                    Item.Text = mRow("ItemA")
                    Item.SubItems.Add(mRow("ItemB"))
                    Item.SubItems.Add(mRow("ItemF"))
                    Item.SubItems.Add(mRow("Amount"))
                    Item.SubItems.Add(mRow("SumMoney"))
                    Me.ListView1.Items.Add(Item)
                Next
            End If
        Catch ex As Exception
            MsgBox("FillControl" & ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            m_Ds.Dispose()
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub GetToolEnable()
        Try
            With MainForm
                .ToolStripAdd.Enabled = AddEnable
                .ToolStripEdit.Enabled = EditEnable And bFind = True
                .ToolStripDelete.Enabled = DeleteEnable And bFind = True
                .ToolStripPrint.Enabled = PrintEnable
                .ToolStripImport.Enabled = ImportEnable
                .ToolStripExport.Enabled = ExportEnable
                .ToolStripFind.Enabled = FindEnable
                .ToolStripFilter.Enabled = True
                .ToolStripCheck.Enabled = CheckEnable
                .ToolStripUnCheck.Enabled = UncheckEnable
                .ToolStripComplete.Enabled = CompleteEnable
                .AddToolStripMenuItem.Enabled = .ToolStripAdd.Enabled
                .Edit1ToolStripMenuItem.Enabled = .ToolStripEdit.Enabled
                .DelToolStripMenuItem.Enabled = .ToolStripDelete.Enabled
                .SaveToolStripMenuItem.Enabled = .ToolStripSave.Enabled
                .CancelToolStripMenuItem.Enabled = .ToolStripCancel.Enabled
                .PrintToolStripMenuItem.Enabled = .ToolStripPrint.Enabled
                .ExportToolStripMenuItem.Enabled = .ToolStripImport.Enabled
                .ImportToolStripMenuItem.Enabled = .ToolStripExport.Enabled
                .FilterToolStripMenuItem.Enabled = .ToolStripFilter.Enabled
            End With

        Catch ex As Exception
            MsgBox("GetToolEnable:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

#End Region


#Region "sub or function"



    Private Sub Frm_PURQuoteManage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try
            BindGridDetail()
            BindMasterGrid()
            Me.Combox1.Visible = False
            Me.Combox2.Visible = False
            cmbStartSite.Visible = False
            cmbEndSite.Visible = False
            Me.Combox1.DropDownStyle = ComboBoxStyle.DropDownList
            Me.Combox2.DropDownStyle = ComboBoxStyle.DropDownList
            Me.cmbStartSite.DropDownStyle = ComboBoxStyle.DropDownList
            Me.cmbEndSite.DropDownStyle = ComboBoxStyle.DropDownList
            Me.dgv.Controls.Add(Me.Combox1)
            Me.dgv.Controls.Add(Me.Combox2)
            Me.dgv.Controls.Add(Me.cmbStartSite)
            Me.dgv.Controls.Add(Me.cmbEndSite)
            LoadDataType()
            SetControlEnable(False)
            With Me.ListView1.Columns
                .Add("編號", 35, HorizontalAlignment.Left)
                .Add("項目名稱", 180, HorizontalAlignment.Left)
                .Add("單價", 50, HorizontalAlignment.Left)
                .Add("數量", 80, HorizontalAlignment.Left)
                .Add("總計", 80, HorizontalAlignment.Left)
            End With
        Catch ex As Exception

        End Try

    End Sub

    Private Sub dgv_CellValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellValidated
        Me.Laberror.Text = ""
        Me.Laberror.Visible = False
        Me.dgv.Rows(e.RowIndex).ErrorText = Nothing
    End Sub

    Private Sub dgv_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgv.CellValidating
        Try

            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
                If e.ColumnIndex = 6 Then
                    Dim testInteger As Double
                    If Me.dgv.CurrentRow.IsNewRow Then
                        Exit Sub
                    End If
                    If Not e.FormattedValue.ToString.Length = 0 Then
                        If Not Double.TryParse(e.FormattedValue.ToString, testInteger) Then
                            Me.Laberror.Text = "無效的輸入!"
                            e.Cancel = True
                        End If
                    End If
                End If
            End If
        Catch ex As Exception
            MsgBox("dgv_CellValidating:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub dgv_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellValueChanged

        Dim KmSum As Double = 0
        Dim UnitPrice As Double = 0
        Dim Amount As Double = 0
        Try
            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then

                If e.ColumnIndex = 6 Then

                    If Not Me.dgv.CurrentRow.Cells("KmSum").Value Is DBNull.Value Then
                        KmSum = Double.Parse(Me.dgv.CurrentRow.Cells("KmSum").Value)
                    End If

                    If Not Me.dgv.CurrentRow.Cells("UnitPrice").Value Is DBNull.Value Then
                        UnitPrice = Double.Parse(Me.dgv.CurrentRow.Cells("UnitPrice").Value)
                    End If

                    If Not Me.dgv.CurrentRow.Cells("Amount").Value Is DBNull.Value Then
                        Amount = Double.Parse(Me.dgv.CurrentRow.Cells("Amount").Value)
                    End If

                    If Not Me.dgv.CurrentRow.Cells("Type1").Value Is DBNull.Value Then
                        Me.dgv.CurrentRow.Cells("SumPrice").Value = Amount * KmSum * UnitPrice
                    End If

                    If Not Me.dgv.CurrentRow.Cells("Type2").Value Is DBNull.Value Then
                        Me.dgv.CurrentRow.Cells("SumPrice").Value = Amount * UnitPrice
                    End If
                End If
            End If
        Catch ex As Exception
            MsgBox("dgv_CellValueChanged:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub dgv_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgv.Click

        Try
            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then

                If Me.dgv.CurrentCell.ColumnIndex = 3 Then
                    Me.cmbStartSite.Visible = False
                    Me.cmbEndSite.Visible = False
                    Combox2.Visible = False
                    Dim Rect As Rectangle = Me.dgv.GetCellDisplayRectangle(Me.dgv.CurrentCell.ColumnIndex, Me.dgv.CurrentCell.RowIndex, False)
                    Dim CurrencyValue As String = Me.dgv.CurrentCell.Value.ToString
                    Combox1.Left = Rect.Left
                    Combox1.Top = Rect.Top
                    Combox1.Width = Rect.Width
                    Combox1.Height = 100
                    Combox1.Visible = True
                ElseIf Me.dgv.CurrentCell.ColumnIndex = 4 Then
                    Me.cmbStartSite.Visible = False
                    Me.cmbEndSite.Visible = False
                    Combox1.Visible = False
                    Dim Rect As Rectangle = Me.dgv.GetCellDisplayRectangle(Me.dgv.CurrentCell.ColumnIndex, Me.dgv.CurrentCell.RowIndex, False)
                    Dim CurrencyValue As String = Me.dgv.CurrentCell.Value.ToString
                    Combox2.Left = Rect.Left
                    Combox2.Top = Rect.Top
                    Combox2.Width = Rect.Width
                    Combox2.Height = 100
                    Combox2.Visible = True
                ElseIf Me.dgv.CurrentCell.ColumnIndex = 0 Then
                    Me.cmbEndSite.Visible = False
                    Combox2.Visible = False
                    Combox1.Visible = False
                    Dim Rect As Rectangle = Me.dgv.GetCellDisplayRectangle(Me.dgv.CurrentCell.ColumnIndex, Me.dgv.CurrentCell.RowIndex, False)
                    Dim CurrencyValue As String = Me.dgv.CurrentCell.Value.ToString
                    cmbStartSite.Left = Rect.Left
                    cmbStartSite.Top = Rect.Top
                    cmbStartSite.Width = Rect.Width
                    cmbStartSite.Height = 100
                    cmbStartSite.Visible = True
                ElseIf Me.dgv.CurrentCell.ColumnIndex = 1 Then
                    Me.cmbStartSite.Visible = False
                    Combox2.Visible = False
                    Combox1.Visible = False
                    Dim Rect As Rectangle = Me.dgv.GetCellDisplayRectangle(Me.dgv.CurrentCell.ColumnIndex, Me.dgv.CurrentCell.RowIndex, False)
                    Dim CurrencyValue As String = Me.dgv.CurrentCell.Value.ToString
                    cmbEndSite.Left = Rect.Left
                    cmbEndSite.Top = Rect.Top
                    cmbEndSite.Width = Rect.Width
                    cmbEndSite.Height = 100
                    cmbEndSite.Visible = True
                Else
                    Combox1.Visible = False
                    Combox2.Visible = False
                    Me.cmbStartSite.Visible = False
                    Me.cmbEndSite.Visible = False
                End If

            End If

        Catch ex As Exception
            MsgBox("dgv_CurrentCellChanged:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub dgv_ColumnWidthChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewColumnEventArgs) Handles dgv.ColumnWidthChanged
        Me.Combox1.Visible = False
        Me.Combox2.Visible = False
        Me.cmbStartSite.Visible = False
        Me.cmbEndSite.Visible = False
    End Sub


    Private Sub dgv_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles dgv.Scroll
        Me.Combox1.Visible = False
        Me.Combox2.Visible = False
        Me.cmbStartSite.Visible = False
        Me.cmbEndSite.Visible = False
    End Sub

    'Private Sub DataGridView2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView2.MouseDown
    '    'If e.Button = Windows.Forms.MouseButtons.Left Then
    '    '    Me.DataGridView2.Capture = False

    '    '    Dim msg As Message = Message.Create(Me.DataGridView2.Handle, WM_NCLBUTTONDOWN, New IntPtr(HTCAPTION), IntPtr.Zero)
    '    '    Me.DefWndProc(msg)
    '    'End If

    '    MouseMoving = True
    '    MouseStartPoint = e.Location
    'End Sub

    'Private Sub DataGridView2_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView2.MouseEnter
    '    Me.Cursor = Cursors.Hand
    'End Sub

    'Private Sub DataGridView2_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView2.MouseLeave
    '    Me.Cursor = Cursors.Default
    'End Sub

    'Private Sub DataGridView2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView2.MouseMove
    '    'If Me.DataGridView2.Left <= Me.Panel3.Left Then
    '    '    Me.Panel3.AutoScroll = True
    '    'End If
    '    'If Me.DataGridView2.Bottom >= Me.Panel3.Bottom Then
    '    '    Me.Panel3.AutoScroll = True
    '    'End If
    '    If MouseMoving = True Then
    '        Me.DataGridView2.Left += e.X - MouseStartPoint.X
    '        Me.DataGridView2.Top += e.Y - MouseStartPoint.Y
    '    End If
    'End Sub

    'Private Sub DataGridView2_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView2.MouseUp
    '    MouseMoving = False
    'End Sub

    'Private Sub TextBox5_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
    '    If e.Data.GetDataPresent(DataFormats.FileDrop) Then
    '        Dim files() As String = CType(e.Data.GetData(DataFormats.FileDrop), String())

    '        If IO.Path.GetExtension(files(0)).ToUpper <> ".TXT" Then
    '            MsgBox("Not a text file")
    '        Else
    '            Me.TextBox5.Text = My.Computer.FileSystem.ReadAllText(files(0))
    '        End If
    '    End If
    'End Sub

    'Private Sub TextBox5_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
    '    If e.Data.GetDataPresent(DataFormats.FileDrop) Then
    '        e.Effect = DragDropEffects.All
    '    Else
    '        e.Effect = DragDropEffects.None
    '    End If
    'End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Combox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Combox1.SelectedIndexChanged

        Try
            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
                If Me.dgv.CurrentRow Is Nothing Then Exit Sub
                If Not Me.dgv.CurrentRow.Cells("Type2").Value Is DBNull.Value Then
                    Me.dgv.CurrentRow.Cells("Type2").Value = ""
                    Me.dgv.CurrentRow.Cells("Type2").Tag = Nothing
                End If
                Me.dgv.CurrentRow.Cells("Type1").Value = Me.Combox1.Text
                Me.dgv.CurrentRow.Cells("Type1").Tag = Me.Combox1.SelectedValue.ToString.Split("^")(1)
                Me.dgv.CurrentRow.Cells("UnitPrice").Value = Me.Combox1.SelectedValue.ToString.Split("^")(2)
                If Not Me.dgv.CurrentRow.Cells("Amount").Value Is DBNull.Value Then
                    Me.dgv.CurrentRow.Cells("SumPrice").Value = Double.Parse(Me.dgv.CurrentRow.Cells("Amount").Value) * Double.Parse(Me.dgv.CurrentRow.Cells("UnitPrice").Value)
                End If
            End If

        Catch ex As Exception
            MsgBox("Combox1_SelectedIndexChanged:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Combox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Combox2.SelectedIndexChanged

        Try

            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
                If Me.dgv.CurrentRow Is Nothing Then Exit Sub
                If Not Me.dgv.CurrentRow.Cells("Type1").Value Is DBNull.Value Then
                    Me.dgv.CurrentRow.Cells("Type1").Value = ""
                    Me.dgv.CurrentRow.Cells("Type1").Tag = Nothing
                End If
                Me.dgv.CurrentRow.Cells("Type2").Value = Me.Combox2.Text
                Me.dgv.CurrentRow.Cells("Type2").Tag = Me.Combox2.SelectedValue.ToString.Split("^")(1)
                Me.dgv.CurrentRow.Cells("UnitPrice").Value = Me.Combox2.SelectedValue.ToString.Split("^")(2)
                If Not Me.dgv.CurrentRow.Cells("Amount").Value Is DBNull.Value Then
                    Me.dgv.CurrentRow.Cells("SumPrice").Value = Double.Parse(Me.dgv.CurrentRow.Cells("Amount").Value) * Double.Parse(Me.dgv.CurrentRow.Cells("UnitPrice").Value)
                End If
            End If

        Catch ex As Exception
            MsgBox("Combox2_SelectedIndexChanged:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnComputer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnComputer.Click

        Try
            If Me.txtServerCount.Text.Length > 0 Then

                Me.ListView1.Items(SelectIndex).SubItems.Add(Me.txtServerCount.Text)
                Me.ListView1.Items(SelectIndex).SubItems.Add(Me.TextBox6.Text)
                Me.txtServerCount.Text = ""
                Me.TextBox6.Text = 0.0
            End If

        Catch ex As Exception
            MsgBox("btnComputer_Click:" & ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

        Try

            For Each Item As ListViewItem In Me.ListView1.Items
                If Item.Selected = True Then
                    SelectIndex = Item.Index
                    Me.TextBox1.Text = Item.SubItems(1).Text
                    Me.TextBox4.Text = Item.SubItems(2).Text
                    If Item.SubItems.Count > 3 Then
                        Me.txtServerCount.Text = Item.SubItems(3).Text
                        Me.TextBox6.Text = Item.SubItems(4).Text
                    End If
                    Exit Sub
                End If
            Next

        Catch ex As Exception
            MsgBox("ListView1_SelectedIndexChanged:" & ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtStoreSqureSum_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtStoreSqureSum.KeyPress

        Try

            Dim Input As String = Me.txtStoreSqureSum.Text.Substring(0, Me.txtStoreSqureSum.SelectionStart) + e.KeyChar + Me.txtStoreSqureSum.Text.Substring(Me.txtStoreSqureSum.SelectionStart, Me.txtStoreSqureSum.SelectionLength)

            If Char.IsControl(e.KeyChar) = True Then
                e.Handled = False
            Else
                e.Handled = Not IsMatch(Input, 6, 7)
            End If


        Catch ex As Exception
            MsgBox("txtStoreSqureSum_KeyPress:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtServerCount_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtServerCount.KeyPress

        Try

            Dim Input As String = Me.txtServerCount.Text.Substring(0, Me.txtServerCount.SelectionStart) + e.KeyChar + Me.txtServerCount.Text.Substring(Me.txtServerCount.SelectionStart, Me.txtServerCount.SelectionLength)

            If e.KeyChar.IsControl(e.KeyChar) Then
                e.Handled = False
            Else
                e.Handled = Not IsMatch(Input, 6, 7)
            End If

        Catch ex As Exception
            MsgBox("txtStoreSqureSum_KeyPress:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtStoreSqureSum_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtStoreSqureSum.TextChanged

        Try
            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
                If Me.txtStorePrice.Text.Length <= 0 Then

                Else
                    If Me.txtStoreSqureSum.Text.Length <= 0 Then
                        Me.txtStoreMoney.Text = 0.0
                    Else
                        Me.txtStoreMoney.Text = Convert.ToDouble(Me.txtStorePrice.Text) * Convert.ToDouble(Me.txtStoreSqureSum.Text)
                    End If
                End If
            End If

        Catch ex As Exception
            MsgBox("txtStoreSqureSum_TextChanged:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtServerCount_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtServerCount.TextChanged

        Try
            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
                If Me.txtServerCount.Text.Length <= 0 Then

                Else
                    If Me.txtServerCount.Text.Length <= 0 Then
                        Me.TextBox6.Text = 0.0
                    Else
                        Me.TextBox6.Text = Convert.ToDouble(Me.TextBox4.Text) * Convert.ToDouble(Me.txtServerCount.Text)
                    End If
                End If
            End If

        Catch ex As Exception
            MsgBox("txtServerCount_TextChanged:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmbStartSite_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbStartSite.SelectedIndexChanged

        Try
            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
                Me.dgv.CurrentRow.Cells("StartSite").Value = Me.cmbStartSite.Text
                Me.dgv.CurrentRow.Cells("StartSite").Tag = Me.cmbStartSite.SelectedValue

            End If
        Catch ex As Exception

        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmbEndSite_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbEndSite.SelectedIndexChanged

        Dim sql As String = ""
        Dim StartSiteTag As String = ""
        Try
            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
                Me.dgv.CurrentRow.Cells("EndSite").Value = Me.cmbEndSite.Text
                Me.dgv.CurrentRow.Cells("EndSite").Tag = Me.cmbEndSite.SelectedValue

                If Me.dgv.CurrentRow.Cells("StartSite").Tag Is DBNull.Value Then
                    MsgBox("開始站點設置不正確，請重新設定！", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                    Exit Sub
                End If
                StartSiteTag = Me.dgv.CurrentRow.Cells("StartSite").Tag.ToString

                sql = "select * from dbo.Lo_DistanceSetup where fStart='" & StartSiteTag & "' and fEnd='" & Me.cmbEndSite.SelectedValue.ToString & "'"
                Dim mTable As New DataTable
                mTable = ReturnDataTable(sql)
                If mTable.Rows.Count > 0 Then
                    Me.dgv.CurrentRow.Cells("KmSum").Value = Double.Parse(mTable.Rows(0).Item("fAccount"))
                Else
                    MsgBox("公裏數沒有設置，請重新設定公裏數！", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                    Exit Sub
                End If
                mTable.Dispose()
            End If
        Catch ex As Exception
            MsgBox("cmbEndSite_SelectedIndexChanged:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub TabControl1_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Selected
        Try
            If Not Me.TabControl1.SelectedTab Is Nothing Then
                Select Case Me.TabControl1.SelectedIndex
                    Case 0

                    Case 1
                        If Me.dgv_Master.Rows.Count > 0 Then
                            Dim fCode As String = Me.dgv_Master.CurrentRow.Cells("QuoteNo").Value
                            FillControl(fCode)
                        End If
                End Select
            End If
        Catch ex As Exception
            MsgBox("TabControl1_Selected:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub TabControl1_Selecting(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting
        Try
            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
                e.Cancel = True
            End If
        Catch ex As Exception
            MsgBox("TabControl1_Selecting:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmbStore_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbStore.SelectedIndexChanged

        Dim sql As String = ""
        Dim Parameter As String = ""
        If Me.cmbStore.SelectedIndex <> -1 Then
            Parameter = Me.cmbStore.SelectedValue.ToString
        End If

        sql = "select b.fcode,b.ServiceTag,b.ItemNoA,b.ItemNoB,mValue=b.fcode+'^'+b.ServiceTag+'^'+b.ItemNoB from Logic_ParameterSetA a inner join dbo.Logic_ParameterSetB b on a.fCode=b.fCode where b.ServiceTag like 'TransSeparate%' and a.StoreCode='" & Parameter & "' and '" & ServerDate & "' between a.StartDate and a.EndDate;"
        sql += "select b.fcode,b.ServiceTag,b.ItemNoA,b.ItemNoB,mValue=b.fcode+'^'+b.ServiceTag+'^'+b.ItemNoB from Logic_ParameterSetA a inner join dbo.Logic_ParameterSetB b on a.fCode=b.fCode where b.ServiceTag='TransWholeCar' and a.StoreCode='" & Parameter & "' and '" & ServerDate & "' between a.StartDate and a.EndDate;"
        sql += "select b.fcode,b.ServiceTag,b.ItemNo,b.ItemNoA,b.ItemNoB from Logic_ParameterSetA a inner join dbo.Logic_ParameterSetB b on a.fCode=b.fCode where a.Storecode='" & Parameter & "' and '" & ServerDate & "' between a.StartDate and a.EndDate;"

        If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then

            Dim m_Ds As New DataSet

            Try
                m_Ds = ReturnDataset(sql)

                If m_Ds.Tables(0).Rows.Count > 0 Then


                    Me.Combox1.DisplayMember = "ItemNoA"
                    Me.Combox1.ValueMember = "mValue"
                    Me.Combox1.DataSource = m_Ds.Tables(0)

                    Me.Combox2.DisplayMember = "ItemNoA"
                    Me.Combox2.ValueMember = "mValue"
                    Me.Combox2.DataSource = m_Ds.Tables(1)

                    Me.ListView1.Items.Clear()
                    m_Ds.RemotingFormat = SerializationFormat.Binary
                    For Each mRow As DataRow In m_Ds.Tables(2).Rows
                        If String.Compare(mRow("ServiceTag").ToString.ToLower, "StoreParameter".ToLower) <> -1 Then
                            Me.txtStorePrice.Text = mRow("ItemNoB")
                            Me.LabStoreTag.Text = mRow("ServiceTag")
                            Me.LabStoreQuote.Text = mRow("fCode")
                        ElseIf String.Compare(mRow("ServiceTag").ToString.ToLower, "ServiceParameter".ToLower) <> -1 Then
                            Dim Item As ListViewItem
                            Item = New ListViewItem
                            Item.Text = mRow("ItemNo")
                            Item.SubItems.Add(mRow("ItemNoA"))
                            Item.SubItems.Add(mRow("ItemNoB"))
                            Me.ListView1.Items.Add(Item)
                        Else
                            Me.txtStorePrice.Text = 0.0
                        End If
                    Next
                Else
                    Me.txtStorePrice.Text = 0.0
                End If

            Catch ex As Exception
                MsgBox("cmbStore_SelectedIndexChanged:" & ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
            Finally
                m_Ds.Dispose()
            End Try

        End If
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnFind_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFind.Click

        Dim sql As String = ""
        Dim Sqlstr As String = ""

        If Me.TxtSelectQutoNo.Text.Length > 0 Then
            Sqlstr += " and QuoteNo='" & Me.TxtSelectQutoNo.Text.Trim & "'"
        End If

        If Me.cmbSelectCustomer.SelectedIndex <> -1 Then
            Sqlstr += " and a.CustomerNo='" & Me.cmbSelectCustomer.SelectedValue.ToString & "'"
        End If

        If Me.cmbSelectStore.SelectedIndex <> -1 Then
            Sqlstr += " and a.Store='" & Me.cmbSelectStore.SelectedValue.ToString & "'"
        End If

        Sqlstr += " and a.CreateDate between '" & FormatDate(Me.DateTimePicker3.Value) & "' and '" & FormatDate(Me.DateTimePicker4.Value) & "'"

        sql = "select a.QuoteNo,CustomerNo=b.Cnname,a.StartDate,a.EndDate,Store=c.fShortName,a.Remark,a.CreateDate,a.CreatePerson,a.CreateComputer from dbo.Logic_QuoteManageA a left join dbo.BSC_Customer b on a.CustomerNo=b.Code "
        sql += "left join dbo.BSC_StoreHouse c on a.Store=c.fCode where 1=1" & Sqlstr

        Dim mTable As New DataTable
        mTable = ReturnDataTable(sql)
        Try
            Me.dgv_Master.DataSource = mTable
            bFind = mTable.Rows.Count > 0
            GetToolEnable()
        Catch ex As Exception
            MsgBox("btnFind_Click" & ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mTable.Dispose()
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub dgv_Master_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgv_Master.DoubleClick
        Me.TabControl1.SelectedIndex = 1
    End Sub

#End Region


End Class
